import { ComponentInterface, ComponentOptions } from "./Component.js";
import Screen from "./Screen.js";

export default class Layout implements ComponentInterface {
  options: ComponentOptions;
  x: number;
  y: number;
  width: number;
  height: number;
  screenX?: number;
  screenY?: number;
  screen?: Screen;
  children: ComponentInterface[]
  context: { screenX: number; screnY: number; };

  constructor(options: ComponentOptions) {
    this.options = options
    this.x = options.x ?? 0
    this.y = options.y ?? 0
    this.width = options.width ?? 0
    this.height = options.height ?? 0
    this.screenX = 0
    this.screenY = 0
  }
  async resize(): Promise<void> {
    await this.layout()
    await Promise.all(this.children.map(async (child) => {
      await child.resize()
    }))
  }

  async init(): Promise<void> {
    await Promise.all(this.children.map(async (child) => {
      child.screen = this.screen
      child.parent = this
      await child.init()
    }))
  }
  async load(): Promise<void> {
    await this.layout()
    await Promise.all(this.children.map(async (child) => {
      await child.load()
    }))
  }
  async unload(): Promise<void> {
    await Promise.all(this.children.map(async (child) => {
      await child.unload()
    }))
  }
  async destroy(): Promise<void> {
    await Promise.all(this.children.map(async (child) => {
      await child.destroy()
    }))
  }
  async update(dt: number): Promise<void> {
    await Promise.all(this.children.map(async (child) => {
      await child.update(dt)
    }))
  }
  async layout(): Promise<void> {
    this.context = {
      screenX: this.screenX ?? 0,
      screnY: this.screenY ?? 0
    }
    await Promise.all(this.children.map(async (child) => {
      await this.layoutChild(child)
    }))
  }

  async layoutChild(child: ComponentInterface): Promise<void> {
    
  }
}

export class Rect {
  left: number
  top: number
  width: number
  height: number
  constructor(x: number, y: number, w: number, h: number) {
    this.left = x
    this.top = y
    this.width = w
    this.height = h
  }
  static fromLTWH(x: number, y: number, w: number, h: number) {
    return new Rect(x, y, w, h)
  }
}

export class Size {
  width: number
  height: number

  constructor(w: number, h: number) {
    this.width = w
    this.height = h
  }
}

export class Point {
  x: number
  y: number

  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }

  clone() {
    return new Point(this.x, this.y)
  }
}

export class Vector {
  dx: number
  dy: number

  constructor(x: number, y: number) {
    this.dx = x
    this.dy = y
  }
}

export class Circle {
  x: number
  y: number
  r: number
  constructor(x: number, y: number, r: number) {
    this.x = x
    this.y = y
    this.r = r
  }
}